/**
 * Created by 57376 on 2017/12/6.
 */
$(document).ready(function() {
//	页面加载自动刷新一次验证码
	$('.toggleauthcode').trigger('click');
})

// 验证第一步 手机号获得焦点事件
$('input[name=phone]').on('focus', function() {
	$('input[name=phone]').css('border-color', '')
	$('.Uname p').addClass('hide');
})
// 验证第一步 验证码获取焦点事件
$('input[name=authcode]').on('focus', function() {
	$('input[name=authcode]').css('border-color', '')
	$('.authcode p').addClass('hide');
})



// 验证第一步 手机号 失去焦点事件
$('input[name=phone]').on('blur', function() {
	var Uphone = $('input[name=phone]').val();
	$.ajax({
		type : "get",
		data : {
			phone : Uphone
		},
		dataType : "json",
		url : root + "/json/users/check_phone",
		success : function(res) {
			if (res.code !== 1001) {
				$('input[name=phone]').css('border-color', 'red')
				$('.Uname p').removeClass('hide');
			} else {
				$('input[name=phone]').css('border-color', 'green')
				$('input[name=phone]').attr('data-id', 'data-name')
			}
		}
	})
})
// 验证第一步 点击切换验证码
$('.toggleauthcode').on('click',function() {
		var t = new Date().getTime();
		$('.img-code').attr('src', root + 'captcha/4?time=' + t);
		setTimeout(function() {
				$.ajax({
					type : "get",
					async : true,
					url : root + "login.txt?time=" + t,
					success : function(result) {
						console.log(result)
						$('input[name=authcode').on('blur',function() {
							if ($('input[name=authcode]').val() !== result) {
									console.log($('input[name=authcode]').val());
									
									$('input[name=authcode]').css('border-color','red')
									$('.authcode p').removeClass('hide');
							} else if ($('input[name=authcode]').val() == result) {
									console.log('验证码正确')
									$('input[name=authcode]').css('border-color','green')
									$('.authcode p').addClass(	'hide');
									$('input[name=authcode]').attr('data-id','data-authcode')
							}
						})
					}
				})
		}, 500)
})

// 第一步 填写账户提交
$('#form_1').on('click', function() {
	/**/
	var nameDataId = $('input[name=phone]').attr('data-id');
	var authcodeDataId = $('input[name=authcode]').attr('data-id');
	if (nameDataId == 'data-name' && authcodeDataId == 'data-authcode') {
		$('.form1').addClass('hide');
		$('.steps span:nth-child(1)').removeClass('span_green');
		$('.steps span:nth-child(1)').addClass('span_green1');
		$('.steps span:nth-child(1) i').removeClass('i_green');
		$('.steps span:nth-child(1) i').addClass('i_green1');
		$('.form2').removeClass('hide');
		$('.steps span:nth-child(2)').addClass('span_green');
		$('.steps span:nth-child(2) i').addClass('i_green');
	} else {
		alert('手机或验证码错误')
	}
	var Uphone = $('input[name=phone]').val();
	$('.phone span').html(Uphone);
	
	$('#messageCode').on('click', function() {
		$.ajax({
			type : "get",
			async : true,
			data : {
				phone : Uphone
			},
			url : root + "json/sms/reg?",
			success : function(res) {
				console.log(res.code);
				resCode = res.code;
				$('#form_2').on('click',function(){
					console.log($('input[name=phoneCode]').val());
					if($('input[name=phoneCode]').val() == resCode){
						/**/
						 $('.form2').addClass('hide');
						 $('.steps span:nth-child(2)').removeClass('span_green');
						 $('.steps span:nth-child(2)').addClass('span_green1');
						 $('.steps span:nth-child(2) i').removeClass('i_green');
						 $('.steps span:nth-child(2) i').addClass('i_green1');
						 $('.form3').removeClass('hide');
						 $('.steps span:nth-child(3)').addClass('span_green');
						 $('.steps span:nth-child(3) i').addClass('i_green');
						 
						 //验证第三步  设置新密码获取焦点事件
							$('input[name=password]').on('focus', function() {
								$('input[name=password]').css('border-color', '')
								$('.Uname p').removeClass('hide');
							})
						 //设置密码失去焦点
						 $('input[name=password]').on('blur',function(){
						        var strPwd1 =  $('input[name=password]').val();
						        //密码必须6~20位.
						        if(strPwd1.length < 6 || strPwd1.length > 20 ){
						        	$('input[name=password]').css('border-color','red');
						        	$('.form3 .Uname p').css('color','red');
						        }else{
						        	$('input[name=password]').css('border-color','green');
						        	$('.form3 .Uname p').addClass('hide');
						        }   
						        //确认密码失去焦点
								 $('input[name=pwd2]').on('blur',function(){
									 var strPwd2 =  $('input[name=pwd2]').val();
									 if(strPwd2 == strPwd1){
										 $('input[name=pwd2]').css('border-color','green');
										 $('.form3 .authcode p').addClass('hide');
										 //发送Ajax 提交	
										$('#form_3').on('click',function(){
											var User_phone=$('input[name=phone]').val();
											var New_password=$('input[name=password]').val();
										    //向后台发送手机号和设置的新密码 										    
										    $.ajax({
												type : "post",
												async : true,
												data : {
													phone : User_phone,
													password:New_password
												},
												url : root + "json/users/forget",
												success : function(res) {
													 	$('.form3').addClass('hide');
													    $('.steps span:nth-child(3)').removeClass('span_green');
													    $('.steps span:nth-child(3)').addClass('span_green1');
													    $('.steps span:nth-child(3) i').removeClass('i_green');
													    $('.steps span:nth-child(3) i').addClass('i_green1');
													    $('.form4').removeClass('hide');
													    $('.steps span:nth-child(4)').addClass('span_green');
													    $('.steps span:nth-child(4) i').addClass('i_green');
												}
											})
										})
										  
									 }else{
										 $('input[name=pwd2]').css('border-color','red');
										 $('.form3 .authcode p').removeClass('hide');
									 }
								 })
						 })
						 
					}else{
						console.log('手机验证码输入有误，请重新获取验证码');
					}
				
				})
			}
		})
		//发送成功后，倒计时60秒
		var timesRun = 60;
		$('#messageCode').attr('disabled', "true");//添加disabled属性  
		var interval = setInterval(function() {
			timesRun--;
			$('#messageCode').html('' + timesRun + 's后重新获取');
			if (timesRun == 0) {
				clearInterval(interval);
				$('#messageCode').removeAttr("disabled"); //移除disabled属性
				$('#messageCode').html('重新获取');
			}
		}, 1000);
	})
})




